<template>
    <div style="width: 100%; height: 100%;">
        <div id="line20250915" style="width: 100%; height: 100%;"></div>
    </div>
</template>
<script setup>
import { watch, defineEmits } from 'vue';
import * as echarts from 'echarts';

// 接受模块的参数
const props = defineProps({
    weekData: {
        type: Array,
        required: true
    },
    abcd: {
        type: String
    },
    maxTemArray: {
        type: Array,
        required: true
    },
    minTemArray: {
        type: Array,
        required: true
    },
    cityName: {
        type: String
    }
})
//接听父模块参数是否变化（deep：开启深度监听）
watch(() => props.weekData, (newVal) => {
    let minTem = props.minTemArray
    let minValue = minTem[0]
    let minXAxis = 0
    for (let i = 0; i < minTem.length; i++) {
        // 比较前，需强制声明为int类型
        minValue = parseInt(minValue)
        let tem = parseInt(minTem[i])
        if (minValue > tem) {
            minValue = tem
            minXAxis = i
        }
    }
    let minYAxis = minValue + 0.5
    let markPointData = [{ name: '周最低', value: minValue, xAxis: minXAxis, yAxis: minYAxis }]

    // 绘制echarts图像
    lineEchartsFun(newVal, props.maxTemArray, props.minTemArray, markPointData, props.cityName)

}, { deep: true })

const lineEchartsFun = (weekData, maxTemArray, minTemArray, markPointData, cityName) => {
    const domLine = document.getElementById('line20250915')
    // getInstanceByDom： 获取 dom 容器上的实例
    const myChartLine = echarts.getInstanceByDom(domLine) || echarts.init(domLine)
    let option = {
        title: {
            text: cityName + '未来一周的天气',
            left: 'left',
            textStyle: { color: '#e2e2e2' }
        },
        tooltip: {
            trigger: 'axis'
        },
        legend: {
            textStyle: { color: '#e2e2e2' }
        },
        toolbox: {
            show: true,
            feature: {
                dataZoom: {
                    yAxisIndex: 'none'
                },
                dataView: { readOnly: false },
                magicType: { type: ['line', 'bar'] },
                restore: {},
                saveAsImage: {}
            }
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            axisLabel: {
                color: '#e2e2e2'
            },
            data: weekData
        },
        yAxis: {
            type: 'value',
            axisLabel: {
                formatter: '{value} °C',
                color: '#e2e2e2'
            }
        },
        series: [
            {
                name: '最高温度',
                type: 'line',
                data: maxTemArray,
                markPoint: {
                    data: [
                        { type: 'max', name: 'Max' },
                        { type: 'min', name: 'Min' }
                    ]
                },
                markLine: {
                    data: [{ type: 'average', name: 'Avg' }]
                }
            },
            {
                name: '最低温度',
                type: 'line',
                data: minTemArray,
                markPoint: {
                    data: markPointData
                },
                markLine: {
                    data: [
                        { type: 'average', name: 'Avg' },
                        [
                            {
                                symbol: 'none',
                                x: '90%',
                                yAxis: 'max'
                            },
                            {
                                symbol: 'circle',
                                label: {
                                    position: 'start',
                                    formatter: 'Max'
                                },
                                type: 'max',
                                name: '最高点'
                            }
                        ]
                    ]
                }
            }
        ]
    };
    myChartLine.clear();
    myChartLine.setOption(option);
    myChartLine.on('click', function (params) {
        appFun(params.name)
    });
}

// 自定义触发事件
const emit = defineEmits(['myGetLeftBottomHandClick']);
const appFun = (data) => {
    // 调用父组件里的自定义事件
    emit('myGetLeftBottomHandClick', data);
};

</script>